package com.zh.note.huawei.onehundred.单词接龙;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

/**
 * @Author: zhanghuan
 * @date: 2023/9/4 19:38
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = Integer.parseInt(sc.nextLine());
        int n = Integer.parseInt(sc.nextLine());
        if (n <= 1 || n >= 20) {
            System.out.println("input error");
        }
        List<String> wordList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            wordList.add(sc.nextLine());
        }

        StringBuilder sb = new StringBuilder();
        sb.append(wordList.get(k));
        wordList.remove(k);
        wordList.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int len1 = o1.length();
                int len2 = o2.length();
                if (len1 != len2) {
                    return len2 - len1;
                } else {
                    return o1.compareTo(o2);
                }
            }
        });
        int length = 0;
        while (length != sb.length()) {
            length = sb.length();
            // 最后一个字符
            String last = sb.substring(sb.length() - 1);
            for (int i = 0; i < wordList.size(); i++) {
                String value = wordList.get(i);
                if (value.startsWith(last)) {
                    sb.append(value);
                    wordList.remove(value);
                    break;
                }
            }
        }
        System.out.println(sb.toString());
    }
}
